package com.chb.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.DictDataMapper;
import com.chb.system.mapper.DictTypeMapper;
import com.chb.system.service.DictDataService;
import com.chb.system.service.UserService;
import com.chb.system.vo.DictDataVo;
import com.hos.entity.sys.DictData;
import com.hos.entity.sys.DictType;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/8 17:30
 * @description:
 * @modified By:
 * @version:
 */
@Service
@Slf4j
public class DictDataServiceImpl implements DictDataService {

    @Resource
    private DictDataMapper dictDataMapper;
    @Resource
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserService userService;


    @Override
    public List<DictData> getDictData(String dictType) {
        try {
            // 1.从缓存中查询数据字段信息
            String dict = redisTemplate.opsForValue().get("dict:" + dictType);
            if (StringUtils.hasText(dict)) {
                return JSON.parseArray(dict, DictData.class);
            }
        } catch (Exception e) {
            log.error("redis服务器取出dict异常");
        }

        LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictData::getDictType, dictType);
        List<DictData> dictData = dictDataMapper.selectList(wrapper);
        // 2.将数据存入缓存中
        redisTemplate.opsForValue().set("dict:" + dictType, JSON.toJSONString(dictData), 1, TimeUnit.DAYS);
        return dictData;
    }

    @Override
    public Page<DictData> findDictDataByCond(DictDataVo dictDataVo) {
        Page<DictData> page = new Page<>(dictDataVo.getPageNum(), dictDataVo.getPageSize());
        LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(dictDataVo.getDictType())) {
            wrapper.eq(DictData::getDictType, dictDataVo.getDictType());
        }
        if (StringUtils.hasText(dictDataVo.getDictLabel())) {
            wrapper.like(DictData::getDictLabel, dictDataVo.getDictLabel());
        }
        if (StringUtils.hasText(dictDataVo.getStatus())) {
            wrapper.eq(DictData::getStatus, dictDataVo.getStatus());
        }
        return dictDataMapper.selectPage(page, wrapper);
    }

    @Override
    public Integer addDictData(DictData dictData) {
        redisTemplate.delete("dict:" + dictData.getDictType());
        dictData.setCreateTime(new Date());
        dictData.setCreateBy(getUserByToken().getUserName());
        return dictDataMapper.insert(dictData);
    }

    @Override
    public Integer deleteDictDataByIds(List<Long> ids) {
        List<DictData> data = dictDataMapper.selectBatchIds(ids);
        for (DictData datum : data) {
            redisTemplate.delete("dict:" + datum.getDictType());
        }
        int i = dictDataMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public DictType getDictDataById(Long dictDataId) {

        return dictTypeMapper.selectById(dictDataId);
    }

    @Override
    public Integer updateDictDate(DictData dictData) {
        redisTemplate.delete("dict" + dictData.getDictType());
        return dictDataMapper.updateById(dictData);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        return userService.findUserByPhone(phone);
    }
}